import os.path, ConfigParser

def parse_file(file_path):
    config = ConfigParser.SafeConfigParser()
    config.read(file_path)
    return config

def base_conf():
    app_dir = os.path.abspath(os.path.dirname(__file__))
    config_file = os.path.join(app_dir, 'settings.conf')
    return parse_file(config_file)

def view_conf(view_name):
    default_conf_file = 'vals.conf'
    default_view_path = 'templates/forum/'
    app_dir = os.path.abspath(os.path.dirname(__file__))

    config_file = os.path.join(app_dir, default_view_path, view_name, default_conf_file)
    return parse_file(config_file)

def get_confs(request, view_name = 'index'):
    base_vals = base_conf()
    default_vals = view_conf('base')
    view_vals = view_conf(view_name)
    tpl_vals = {}
    tpl_vals['rules'] = []

    tpl_vals['title'] = base_vals.get('common', 'title').strip('"')
    tpl_vals['head'] = base_vals.get('common', 'head').strip('"')

    if view_vals.has_section('rules'):
        for index, rule in view_vals.items('rules'):
            tpl_vals['rules'].append(rule.strip('"'))

    tpl_vals['user'] = request.user

    return tpl_vals
